<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <script src="./socket.io.js"></script>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="box" :style="styleObj" ref="mybox" @mousedown="downfn"></div>
    </div>
    <script>
        let socket = io.connect();
        new Vue({
            el: "#app",
            data: {
                message: "数据",
                styleObj:{
                    left:"0px",
                    top:"0px"
                }
            },
            mounted:function(){
                let boxEle = this.$refs.mybox;
                socket.on("sendClientPos",res=>{
                    console.log(res);
                    this.styleObj.left = res.x;
                    this.styleObj.top = res.y;
                    // boxEle.style.left = res.x;
                    // boxEle.style.top = res.y;
                })
            },
            methods: {
                downfn(e) {
                    // let target = e.target;
                    // console.log(target)
                    let boxEle = this.$refs.mybox;
                    // console.log(boxEle);
                    let x = e.clientX - boxEle.offsetLeft;
                    let y = e.clientY - boxEle.offsetTop;
                    boxEle.onmousemove = function (e) {
                        let xx = e.clientX;
                        let yy = e.clientY;
                        this.style.left = xx - x + "px";
                        this.style.top = yy - y + "px";
                        socket.emit("sendpos",{x:this.style.left,y: this.style.top})
                    }
                    boxEle.onmouseup = function(){
                        this.onmousemove  = "";
                    }
                }
            }
        })

    </script>
</body>

</html>